package com.platform.controller;

import com.platform.entity.StudyEntity;
import com.platform.service.StudyService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 学习模块
 *
 */
@RestController
@RequestMapping("study")
public class StudyController {
    @Autowired
    private StudyService studyService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("study:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<StudyEntity> studyList = studyService.queryList(query);
        int total = studyService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(studyList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("study:info")
    public R info(@PathVariable("id") Integer id) {
        StudyEntity study = studyService.queryObject(id);

        return R.ok().put("study", study);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("study:save")
    public R save(@RequestBody StudyEntity study) {
        studyService.save(study);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("study:update")
    public R update(@RequestBody StudyEntity study) {
        studyService.update(study);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("study:delete")
    public R delete(@RequestBody Integer[] ids) {
        studyService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<StudyEntity> list = studyService.queryList(params);
        return R.ok().put("list", list);
    }
}
